package com.itheima.exerciseTest.method30_39.method37;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

//        Constructor类用于创建对象的方法
public class Demo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//        T newInstance(Object...initargs)	根据指定的构造方法创建对象
//        setAccessible(boolean flag)     	设置为true,表示取消访问检查

        // method1();

        // method2();

        // method3();

        // method4();

    }

    private static void method4() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class className = Class.forName("com.itheima.exerciseTest.method30_39.method37.Student");

        Constructor declaredConstructor = className.getDeclaredConstructor(String.class);

        declaredConstructor.setAccessible(true);

        Student student = (Student) declaredConstructor.newInstance("张学友");

        System.out.println(student);
    }

    private static void method3() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class className = Class.forName("com.itheima.exerciseTest.method30_39.method37.Student");

        Student student = (Student) className.newInstance();

        System.out.println(student);
    }

    private static void method2() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class className = Student.class;

        Constructor constructor = className.getConstructor();

        // constructor.setAccessible(true);

        Student student = (Student) constructor.newInstance();

        System.out.println(student);
    }

    private static void method1() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class className = Class.forName("com.itheima.exerciseTest.method30_39.method37.Student");

        Constructor constructor = className.getConstructor(String.class, int.class);

        Student student = (Student) constructor.newInstance("张三", 23);

        System.out.println(student);
    }
}
